<?php
declare (strict_types=1);

namespace app\common\model;

class Account extends Base
{
    const CONSUM_DIRECT = 101;
    const CONSUM_INDIRECT = 102;
    const CONSUM_DISTRICT = 103;
    const CONSUM_CITY = 104;
    const CONSUM_SUPPLY_SHOPER = 111;
    const CONSUM_SUPPLY_LEADER = 112;
    const CONSUM_SUPPLY_DISTRICT = 113;
    const CONSUM_SUPPLY_CITY = 114;

    const CONSUM_SHARE_SHOPER = 121;
    const CONSUM_SHARE_LEADER = 122;
    const CONSUM_SHARE_DISTRICT = 123;
    const CONSUM_SHARE_CITY = 124;

    const SPK_DEALER_SHOPER = 201;
    const SPK_DEALER_SHOPER_HIGH = 202;
    const SPK_DEALER_LEADER = 203;
    const SPK_DEALER_DISTRICT = 204;
    const SPK_DEALER_CITY = 205;

    const SPK_SUPPLY_SHOPER = 211;
    const SPK_SUPPLY_LEADER = 212;
    const SPK_SUPPLY_DISTRICT = 213;
    const SPK_SUPPLY_CITY = 214;

    const SPK_AGENT_DISTRICT = 221;
    const SPK_AGENT_CITY = 222;

    const SPK_SPECIAL = 231;

    const VIP_DIRECT = 301;
    const VIP_INDIRECT = 302;

    const VIDEO_LOOK = 401;
    const VIDEO_DIRECT = 402;

    const SIGNIN = 501;
    const REGISTER = 502;
    const SHARE = 503;

    protected $name = 'user_account';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile,city');
    }

    public function take()
    {
        return $this->hasOne(User::class, 'id', 'source')->field('id,code,nickname,mobile');
    }

    public function getTypeAttr($value)
    {
        return get_pay_way($value);
    }

    public static function _all($map, $param, $hasOne, $like = [], $order = 'create_time,desc', $append = [], $filed = '*', $export = false): array
    {
        try {
            $page = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;

            !$order && $order = 'create_time,desc';

            if (!isset($map['is_del'])) {
                $map['is_del'] = 0;
            }
            $is_pasture = null;
            if (isset($map['is_pasture'])) {
                Account::whereIn('type', ['rabbit_coupon', 'ranch_points', 'ranch_bonus'])->where('is_pasture', 0)->save(['is_pasture' => 1]);
                $is_pasture = $map['is_pasture'] == 1 ?? 0;
                unset($map['is_pasture']);
            }
            $is_tillage = null;
            if (isset($map['is_tillage'])) {
                $is_tillage = $map['is_tillage'] == 1 ?? 0;
                unset($map['is_tillage']);
            }

            $pool_id = 0;
            if (isset($map['pool_id'])) {
                $pool_id = $map['pool_id'];
                unset($map['pool_id']);
            }

            $contract_pool_id = 0;
            if (isset($map['contract_pool_id'])) {
                $contract_pool_id = $map['contract_pool_id'];
                unset($map['contract_pool_id']);
            }

            $amount_min = $amount_max = null;
            if (!empty($param['amount_min'])) {
                $amount_min = $param['amount_min'];
                unset($map['amount_min']);
            }
            if (!empty($param['amount_max'])) {
                $amount_max = $param['amount_max'];
                unset($map['amount_max']);
            }

            $that = parent::where($map);

            if ($is_pasture == 1) {
                $that = $that->whereIn('type', ['rabbit_coupon', 'ranch_points', 'ranch_bonus']);
            }elseif ($is_tillage == 1) {
                $that = $that->whereIn('type', ['rabbit_vote', 'rabbit_chain', 'rabbit_baby', 'cloud_points']);
            } else {
                $that = $that->whereNotIn('type', ['rabbit_coupon', 'ranch_points', 'ranch_bonus', 'rabbit_vote', 'rabbit_chain', 'rabbit_baby', 'cloud_points']);
            }

            if ($pool_id && in_array($pool_id, [1, 2, 3, 4, 5, 6, 7, 8])) {
                if (6 == $pool_id) {
                    $category = self::CONSUM_SHARE_LEADER;
                } elseif (7 == $pool_id) {
                    $category = self::CONSUM_SHARE_DISTRICT;
                } elseif (8 == $pool_id) {
                    $category = self::CONSUM_SHARE_CITY;
                } else {
                    $category = self::CONSUM_SHARE_SHOPER;
                }
                $that = $that->where('category', $category);
            }

            if ($contract_pool_id && in_array($contract_pool_id, [1, 2, 3])) {
                if (1 == $contract_pool_id) {
                    $category = 1009;
                } elseif (2 == $contract_pool_id) {
                    $category = 1010;
                } else {
                    $category = 1011;
                }
                $that = $that->where('category', $category);
            }

            if ($amount_min) $that->where('amount', '>=', $amount_min);
            if ($amount_max) $that->where('amount', '<=', $amount_max);

            if ($hasOne) {
                $that = $that->with($hasOne);
            }

            if (isset($param['times']) && $param['times']) {
                $times = explode(' - ', $param['times']);
                $that->whereBetweenTime('create_time', strtotime($times[0]), strtotime($times[1]));
            }

            if (isset($param['otherTimes']) && $param['otherTimes']) {
                foreach ($param['otherTimes'] as $k => $v) {
                    if (empty($v)) continue;
                    $decode = explode(' - ', $v);
                    $that->whereBetweenTime($k, strtotime($decode[0]), strtotime($decode[1]));
                }
            }

            foreach ($like as $v) {
                foreach ($v as $key => $vo) {
                    $that->whereLike($key, '%' . trim($vo) . '%');
                }
            }
            $order = explode(',', $order);
            if (!$export) {
                $list = $that->page(intval($page), intval($limit))->field($filed)->order($order[0], $order[1])->select();

                if (!empty($append)) {
                    $list->append($append);
                }
                return ['code' => 0, 'data' => $list, 'count' => getCount($that->fetchSql()->select())];
            } else {

                $list = $that->field($filed)->order($order[0], $order[1])->select();
                if (!empty($append)) {
                    $list->append($append);
                }
                return ['code' => 0, 'data' => $list];
            }

        } catch (\Exception $e) {
            dump($e);
            die;
            return [];
        }
    }

    public static function _adds($id, $type, $amount, $intros, $fx, $source = 0, $category = 0, $is_grant = 2, $is_bonus = 0, $is_user = 0)
    {
        try {
            $pasture = ['rabbit_coupon', 'ranch_points', 'ranch_bonus'];
            $tillage = ['rabbit_vote', 'rabbit_chain', 'rabbit_baby', 'cloud_points'];
            $village = ['village_bao', 'village_piao', 'village_lian', 'village_fen'];
            if (is_array($intros)) {
                $intro = $intros[0];
                $msg = $intros[1];
            } else {
                $intro = $intros;
                $msg = '';
            }
            $assets = Assets::where('uid', $id)->find();
            $after = $fx == '+' ? floatval($assets->$type) + floatval($amount) : floatval($assets->$type) - floatval($amount);
            parent::create([
                'uid' => $id,
                'type' => $type,
                'intro' => $intro,
                'amount' => $amount,
                'front' => $assets->$type,
                'after' => $after,
                'fx' => $fx,
                'create_time' => time(),
                'create_date' => date('Y-m-d'),
                'source' => $source,
                'category' => $category,
                'is_grant' => $is_grant,
                'is_bonus' => $is_bonus,
                'is_user' => $is_user,
                'is_admin' => $intro == '后台充值' || $intro == '后台充值' ? 1 : 0,
                'msg' => $msg,
                'is_pasture' => in_array($type, $pasture)? 1 : 0,
                'is_tillage' => in_array($type, $tillage)? 1 : 0,
                'is_village' => in_array($type, $village)? 1 : 0,
            ]);

            $assets->$type = $after;
            $assets->save();
            return true;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}
